package com.sunday.volunteer.admin.ui.index;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.event.EventBus;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.UIAlertView;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.event.ExitApp;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.volunteer.admin.BaseApp;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.adapter.group.GroupApplyRecycleAdapter;
import com.sunday.volunteer.admin.entity.ApplyMember;
import com.sunday.volunteer.admin.entity.Member;
import com.sunday.volunteer.admin.http.VolunteerClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 成员申请
* */
public class GroupApplyActivity extends BaseActivity {
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private Member member;

    private LinearLayoutManager manager;
    private GroupApplyRecycleAdapter adapter;

    private List<ApplyMember> applyMemberList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_aply);
        ButterKnife.bind(this);
        titleView.setText("成员申请");
        member = BaseApp.getInstance().getMember();
        initView();
    }

    private void initView(){
        manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new GroupApplyRecycleAdapter(mContext,applyMemberList);
        recyclerView.setAdapter(adapter);
        refreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        adapter.setItemClickListener(new GroupApplyRecycleAdapter.ItemClickListener() {
            @Override
            public void itemClick(View v, int position,long id) {
                int v_id = v.getId();
                switch (v_id){
                    case R.id.l_main:
                        break;
                    case R.id.tv_agree:
                        showDialog("通过审核？",true,id);
                        break;
                    case R.id.tv_refuse:
                        showDialog("确认拒绝？",false,id);
                        break;
                }
            }
        });
        getData();
        handleMore();
    }

    //获取申请
    private void getData(){
        showLoadingDialog(0);
        Call<ResultDO<List<ApplyMember>>> call = VolunteerClient.getVolunteerAdapter().getRecords(member.getId(),pageNo,pageSize);
        call.enqueue(new Callback<ResultDO<List<ApplyMember>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<ApplyMember>>> call, Response<ResultDO<List<ApplyMember>>> response) {
                dissMissDialog();
                refreshLayout.setRefreshing(false);
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (pageNo==1){
                        applyMemberList.clear();
                    }
                    applyMemberList.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<ApplyMember>>> call, Throwable t) {
                dissMissDialog();
                refreshLayout.setRefreshing(false);
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    private int pageNo = 1;
    private int pageSize = 20;
    private int lastVisibleItem;
    private boolean isCanloadMore = false;
    private void handleMore(){
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                getData();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = manager.findLastVisibleItemPosition();
                isCanloadMore = dy>0;
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount() && isCanloadMore) {
                    pageNo++;
                    getData();
                }
            }
        });
    }

    //处理申请showDialog
    private void showDialog(String str, final boolean bool, final long applyId) {
        final UIAlertView delDialog = new UIAlertView(mContext, "温馨提示", str,
                "取消", "确定");
        delDialog.setClicklistener(new UIAlertView.ClickListenerInterface() {
                                       @Override
                                       public void doLeft() {
                                           delDialog.dismiss();
                                       }

                                       @Override
                                       public void doRight() {
                                           delDialog.dismiss();
                                           doApply(bool,applyId);
                                       }
                                   }
        );
        delDialog.show();
    }

    //处理申请
    private void doApply(final boolean bool, long applyId){
        showLoadingDialog(0);
        Call<ResultDO> call = VolunteerClient.getVolunteerAdapter().handleJoin(member.getId(),applyId,bool);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (bool){
                        ToastUtils.showToast(mContext, "通过审核");
                    }else {
                        ToastUtils.showToast(mContext, "已拒绝");
                    }
                    getData();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }
}
